package com.guigu.userservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common.utils.R;
import com.guigu.userservice.entity.Businessman;
import com.guigu.userservice.entity.BusinessmanEquipment;
import com.guigu.userservice.service.BusinessmanEquipmentService;
import com.guigu.userservice.service.BusinessmanService;
import com.guigu.userservice.vo.BusinessmanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 业务员表  前端控制器
 * </p>
 *
 * @author zcy
 * @since 2025-10-14
 */
@RestController
@RequestMapping("/user/businessman")
public class BusinessmanController {
    @Autowired
    private BusinessmanService businessmanService;

    @Autowired
    private BusinessmanEquipmentService businessmanEquipmentService;

    @PostMapping("queryBusinessmanByVo/{pageNum}/{pageSize}")
    public R queryBusinessmanByVo(@PathVariable("pageNum") int pageNum,
                                  @PathVariable("pageSize") int pageSize,
                                  @RequestBody BusinessmanVo businessmanVo) {
        return businessmanService.queryBusinessmanPageByVo(pageNum, pageSize, businessmanVo);
    }

    @GetMapping("queryBusinessmanById/{id}")
    public R queryBusinessmanById(@PathVariable("id") Integer id) {
        Businessman businessman = businessmanService.getById(id);
        return R.ok().data("businessman", businessman);
    }

    @PostMapping("queryByVo")
    public R queryBusinessmanByVo(@RequestBody BusinessmanVo businessmanVo) {
        return businessmanService.queryBusinessmanByVo(businessmanVo);
    }

    @GetMapping("queryBusinessmanList")
    public R queryBusinessmanList() {
        List<Businessman> list = businessmanService.list();
        return R.ok().data("list", list);
    }
    @PutMapping("updateStatus/{id}/{status}")
    public R updateStatus(@PathVariable("id") Integer id,
                          @PathVariable("status") Integer status) {
        if (status != 0 && status != 1) {
            return R.error().message("参数错误");
        }
        return businessmanService.updateStatus(id, status);
    }

    //设备详情页显示业务员的类型方式
    @GetMapping("queryBusinessmanType/{id}")
    public R queryBusinessmanType(@PathVariable("id") Integer id) {
        QueryWrapper<BusinessmanEquipment> wrapper = new QueryWrapper<>();
        wrapper.eq("equipment_id", id);
        wrapper.eq("status", 1);
        BusinessmanEquipment relation = businessmanEquipmentService.getOne(wrapper);

        if (relation == null) {
            return R.ok().data("businessmanInfo", null).message("该设备暂无负责业务员");
        }

        // 查询业务员信息
        Businessman businessman = businessmanService.getById(relation.getBusinessmanId());
        if (businessman == null || businessman.getStatus() != 1) {
            return R.ok().data("businessmanInfo", null).message("业务员不存在或已禁用");
        }

        // 组装返回数据
        Map<String, Object> businessmanInfo = new HashMap<>();
        businessmanInfo.put("businessmanId", businessman.getBusinessmanId());
        businessmanInfo.put("businessmanName", businessman.getName());
        businessmanInfo.put("businessmanPhone", businessman.getPhone());
        businessmanInfo.put("department", businessman.getDepartment());
        businessmanInfo.put("responsibleArea", businessman.getResponsibleArea());
        businessmanInfo.put("responsibilityType", relation.getResponsibilityType());
        businessmanInfo.put("assignTime", relation.getAssignTime());

        return R.ok().data("businessmanInfo", businessmanInfo);
    }

    //修改密码
    @PutMapping("passWordChange/{id}/{newPassWord}")
    public R passWordChange(@PathVariable("id") Integer id,
                            @PathVariable("newPassWord") String newPassWord) {
        return businessmanService.passWordChange(id, newPassWord);
    }

    @PostMapping("addBusinessman")
    public R addBusinessman(@RequestBody Businessman businessman) {
        QueryWrapper<Businessman> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", businessman.getPhone());
        Businessman one = businessmanService.getOne(wrapper);
        if (one != null) {
            return R.error().message("该手机号已注册");
        }
        businessman.setStatus(1);
        businessmanService.save(businessman);
        return R.ok();
    }

}
